package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.GridLayoutManager
import com.luck.picture.lib.entity.LocalMedia
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityFeedBackBinding
import com.starsea.book.ui.newpage.adapter.ImageSelAdapter
import com.starsea.book.ui.newpage.entity.ImageSelEntity
import com.starsea.book.ui.newpage.utils.PicSelUtils
import com.starsea.book.ui.newpage.viewmodel.UserViewModel
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-08-25 14 GMT+8
 * @desc :
 */
class FeedbackActivity : VMBaseActivity<ActivityFeedBackBinding, UserViewModel>() {

    lateinit var mAdapter: ImageSelAdapter
    var selectList: MutableList<LocalMedia> = arrayListOf()
    var uploadList: MutableList<String> = arrayListOf()


    override val viewModel by viewModels<UserViewModel>()

    override val binding by viewBinding(ActivityFeedBackBinding::inflate)

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        binding.titleBarView.setOnLeftTextClickListener {
            finish()
        }

        mAdapter = ImageSelAdapter(this)
        { value: ImageSelEntity, position: Int, type: Int ->
            if (type == 3) {
                if (selectList.size == 6) {
                    mAdapter.add(ImageSelEntity(ImageSelEntity.KEY_ORDER_IMAGE, null))
                }
                mAdapter.removeAt(position)
                selectList.removeAt(position)
            } else if (type == 2) {
                if (mAdapter.getItem(position)!!.localMedia == null) {
                    PicSelUtils.selPic(
                        max = 6,
                        this,
                        selList = selectList
                    ) { result ->
                        selectList.clear()
                        result!!.forEach {
                            selectList.add(it!!)
                        }
//            binding.tvContentNumber.text = "${selectList.size}/6"
                        addImage()
                    }
                }
            }
        }


        mAdapter.addOnItemChildClickListener(R.id.iv_img) { _, _, position ->
            if (mAdapter.getItem(position)!!.localMedia == null) {
                PicSelUtils.selPic(
                    max = 6,
                    this,
                    selList = selectList
                ) { result ->
                    selectList.clear()
                    result!!.forEach {
                        selectList.add(it!!)
                    }
                    addImage()
                }
            }
        }

        mAdapter.addOnItemChildClickListener(R.id.iv_delete) { _, _, position ->
            if (selectList.size == 6) {
                mAdapter.add(ImageSelEntity(ImageSelEntity.KEY_ORDER_IMAGE, null))
            }
            mAdapter.removeAt(position)
            selectList.removeAt(position)
        }


        binding.rvPic.layoutManager = GridLayoutManager(this, 3)
        binding.rvPic.adapter = mAdapter
        mAdapter.submitList(viewModel.getImgList())
        binding.etContent.addTextChangedListener {
            binding.tvNumText.text = "${it.toString().length}/400"
        }


        binding.rtvCommit.setOnClickListener {
            val content = binding.etContent.text.toString()
            if (TextUtils.isEmpty(content)) {
                toastOnUi("请输入您遇到的问题或建议")
                return@setOnClickListener
            }
            showLoading()

            uploadList.clear()
            if (selectList.isEmpty()) {
                viewModel.uploadFeedback(
                    content,
                    "",
                    binding.etPhone.text.toString(),
                    binding.etQq.text.toString()
                )
            } else {
                selectList.forEach {
                    viewModel.uploadImg("", it.realPath)
                }
            }
        }

        viewModel.uploadImgResult.observe(this) {
            if (it.status == 200) {
                uploadList.add(it.data ?: "")
                if (uploadList.size == selectList.size) {
                    val content = binding.etContent.text.toString()
                    var img = ""
                    uploadList.forEach { img += it + "," }
                    viewModel.uploadFeedback(
                        content, img.substring(0, img.length - 1), binding.etPhone.text.toString(),
                        binding.etQq.text.toString()
                    )
                }
            } else {
                toastOnUi(it.msg)
            }
        }

        viewModel.uploadSucResult.observe(this) {
            if (it.status == 200) {
                dismissLoading()
                toastOnUi("反馈成功")
                finish()
            } else {
                toastOnUi(it.msg)
            }
        }

    }


    private fun addImage() {
        val list: MutableList<ImageSelEntity> = ArrayList()
        for (i in selectList.indices) {
            list.add(ImageSelEntity(ImageSelEntity.KEY_ORDER_IMAGE, selectList[i]))
        }
        if (selectList.size < 6) {
            list.add(ImageSelEntity(ImageSelEntity.KEY_ORDER_IMAGE, null))
        }
        mAdapter.submitList(list)

    }


}